// 该文件用于创建Vuex中最为核心的store
import Vue from "vue";
import Vuex from 'vuex'
// 使用插件
Vue.use(Vuex)

const countOptions = {
    // 命名空间namespaced: true
    namespaced: true,
    actions: {
        addOdd(context, val) {
            console.log("actions addOdd被调用了…………", context, val)
            if (context.state.sum % 2) {
                context.commit('ADD', val)
            }
        },
        addWait(context, val) {
            console.log("actions addWait被调用了…………", context, val)
            setTimeout(() => {
                context.commit('ADD', val)
            })
        },
    },
    mutations: {
        ADD(state, val) {
            console.log("mutations ADD被调用了…………", state, val)
            state.sum += val
        },
        CUTE(state, val) {
            console.log("mutations CUTE被调用了…………", state, val)
            state.sum -= val
        },
    },
    state: {
        sum: 0,
        school: 'shanghai',
        subject: 'vue',
    },
    getters: {
        bigSum(state) {
            return state.sum * 10
        }
    },
}

const personOptions = {
    // 命名空间namespaced: true
    namespaced: true,
    actions: {},
    mutations: {
        ADD_PERSON(state, personObj) {
            console.log("mutations ADD_PERSON被调用了…………", state, personObj)
            state.personList.unshift(personObj)
        }
    },
    state: {
        personList: [
            {id: "001", name: 'zhaangsan'}
        ],
    },
    getters: {},
}

// 创建store并到处store
export default new Vuex.Store({
    modules: {
        countOptions,
        personOptions,
    }
})
